﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using umbraco.BusinessLogic.Actions;
using umbraco.cms.presentation.Trees;
using umbraco.interfaces;
using umbraco.cms.businesslogic.language;
using Commerce.Common;
using System.Xml;

namespace CommerceForUmbraco.adminSection.trees
{
    public class loadProductOptionsTree : BaseTree
    {
        public loadProductOptionsTree(string application) : base(application) { }

        protected override void CreateRootNode(ref XmlTreeNode rootNode)
        {
            rootNode.Icon = FolderIcon;
            rootNode.OpenIcon = FolderIconOpen;
            rootNode.NodeType = "init" + TreeAlias;
            rootNode.NodeID = "init";
        }

        public override void RenderJS(ref StringBuilder Javascript)
        {
            Javascript.Append(
                @"
                    function openProductOptionTemplates(id){
                        parent.right.document.location.href = 'commerce/editProductOptionTemplate.aspx?id=' + id;
                    }                   
                    function openProductOptionItems(id){
                        parent.right.document.location.href = 'commerce/editProductOptionItem.aspx?id=' + id;
                    }                   
                ");
        }

        public override void Render(ref XmlTree tree)
        {
            if (this.NodeKey == string.Empty)
            {
                Commerce.Common.AttributeTemplateCollection productOptionTemplates = null;
                productOptionTemplates = new AttributeTemplateCollection().OrderByAsc("attributeName").Load();

                foreach (AttributeTemplate t in productOptionTemplates)
                {
                    XmlTreeNode xNode = XmlTreeNode.Create(this);

                    xNode.NodeID = t.TemplateID.ToString();
                    xNode.Text = t.AttributeName;
                    xNode.Action = "javascript:openProductOptionTemplates(" + t.TemplateID + ");";
                    xNode.Icon = "statistik.gif";
                    xNode.OpenIcon = "statistik.gif";
                    xNode.NodeType = "productOption";
                    
                    xNode.Menu.Clear();
                    xNode.Menu.AddRange(new List<IAction> { ActionRefresh.Instance, ActionNew.Instance, ActionDelete.Instance });

                    TreeService treeService = new TreeService(-1, TreeAlias, ShowContextMenu, IsDialog, DialogMode, app, string.Format("{0}", t.TemplateID));

                    Commerce.Common.AttributeItemCollection productOptionItems = new AttributeItemCollection();
                    productOptionItems.Where("TemplateID", t.TemplateID);
                    productOptionItems.Load();

                    xNode.Source = productOptionItems.Count > 0 ? treeService.GetServiceUrl() : string.Empty;

                    tree.Add(xNode);
                }
            }
            else
            {
                Commerce.Common.AttributeItemCollection productOptionItems = new AttributeItemCollection();
                productOptionItems.Where("TemplateID", this.NodeKey);
                productOptionItems.OrderByAsc("SortOrder");
                productOptionItems.Load();

                foreach (AttributeItem i in productOptionItems)
                {
                    XmlTreeNode xNode = XmlTreeNode.Create(this);

                    xNode.NodeID = i.AttributeItemId.ToString();
                    xNode.Text = i.Name;
                    xNode.Action = "javascript:openProductOptionItems(" + i.AttributeItemId + ");";
                    xNode.Icon = "statistik.gif";
                    xNode.OpenIcon = "statistik.gif";
                    xNode.NodeType = "productOptionItem";

                    xNode.Menu.Clear();
                    xNode.Menu.AddRange(new List<IAction> { ActionRefresh.Instance, ActionDelete.Instance });

                    xNode.Source = string.Empty;

                    tree.Add(xNode);
                }
            }
        }
    }
}